<?php

class Sms extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_sms':
	 * @var string $ID
	 * @var string $Text_Category
	 * @var string $Text_Offer
	 * @var string $Text_Other
	 * @var string $PhoneNumber
	 * @var string $DateTime
	 * @var integer $status
	 */
	const STATUS_PENDING=1;
	const STATUS_APPROVED=2;
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_sms';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, Text_Category, Text_Offer, Text_Other, PhoneNumber, DateTime, status', 'required'),
			array('id, status', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, Text_Category, Text_Offer, Text_Other, PhoneNumber, DateTime, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'Id',
			'Text_Category' => 'Text Category',
			'Text_Offer' => 'Text Offer',
			'Text_Other' => 'Pesan',
			'PhoneNumber' => 'Phone Number',
			'DateTime' => 'Date Time',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
        public function approve()
	{
		$this->status=Sms::STATUS_APPROVED;
		$this->update(array('status'));
	}

        public function getPendingSmsCount()
	{
		return $this->count('status='.self::STATUS_PENDING);
	}

        public function findRecentSms($limit=20)
	{
		return $this->findAll(array(
			'condition'=>'status='.self::STATUS_APPROVED,
			'order'=>'DateTime DESC',
			'limit'=>$limit,
		));
	}

	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID',$this->ID,true);

		$criteria->compare('Text_Category',$this->Text_Category,true);

		$criteria->compare('Text_Offer',$this->Text_Offer,true);

		$criteria->compare('Text_Other',$this->Text_Other,true);

		$criteria->compare('PhoneNumber',$this->PhoneNumber,true);

		$criteria->compare('DateTime',$this->DateTime,true);

		$criteria->compare('status',$this->status);

		return new CActiveDataProvider('Sms', array(
			'criteria'=>$criteria,
		));
	}

        public function shortenText($text,$maxlength) {
        if (strlen($text) > $maxlength) {
            $text = $text.' ';
            $text = substr($text,0,$maxlength);
            $text = substr($text,0,strrpos($text,' '));
            $text = $text.'...';
        }
        return $text;
    }

}